<template>
  <div class="detail">
    <!-- 商品轮播图区域 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <swipper :lunbotuList="lunbotuList" :isfull="false"></swipper>
        </div>
      </div>
    </div>
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballflag" ref="ball"></div>
    </transition>
    <!-- 商品购买区域 -->
    <div class="mui-card" v-for="item in goodsInfo" :key="item.title">
      <div class="mui-card-header">{{item.title}}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价:
            <del>￥{{item.market_price}}</del>
            销售价:
            <span class="now">￥{{item.sell_price}}</span>
          </p>
          <p>
            购买数量:
            <numbox @getCount="getSelectedCount" :max="goodsInfo.stock_quantity"></numbox>
          </p>
          <p>
            <mt-button type="primary" @click="addShopCar">加入购物车</mt-button>
            <mt-button type="danger">立即购买</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 商品详情区域 -->
    <div class="mui-card" v-for="item in goodsInfo" :key="item.id">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p>商品货号：{{item.goods_no}}</p>
          <p>库存情况：{{item.stock_quantity}}</p>
          <p>上架时间：{{item.add_time}}</p>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" plain size="large" @click="getDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" plain size="large" @click="getComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import swipper from "../subcomponents/swipper.vue";
import numbox from "../subcomponents/goodsInfo_numbox.vue";
export default {
  data() {
    return {
      lunbotuList: [],
      goodsInfo: [],
      id: this.$route.params.id,
      ballflag: false,
      selectedCount: 1 //加入购物车的数量刚开始默认为1
    };
  },
  created() {
    this.getLunbo();
    this.getGoodsInfo();
  },
  methods: {
    getLunbo() {
      this.$http.get("src/json/goodsPhoto" + this.id + ".json").then(result => {
        // if(result.body.status === 0){
        result.body.message.forEach(element => {
          element.image = element.src;
        });
        this.lunbotuList = result.body.message;
        // }
      });
    },
    getGoodsInfo() {
      this.$http.get("src/json/goodsInfo" + this.id + ".json").then(result => {
        // if(result.body.status === 0){
        this.goodsInfo = result.body.message;
        // }
      });
    },
    getDesc(id) {
      this.$router.push({ name: "goodsdesc", params: { id } });
    },
    getComment(id) {
      this.$router.push({ name: "goodscomment", params: { id } });
    },
    addShopCar() { 
      this.ballflag = !this.ballflag;

      var goodsinfo = {
        id: this.id,
        count: this.selectedCount,
        price: this.goodsInfo[0].sell_price,
        selected: true
      };
      this.$store.commit("addToCar",goodsinfo)
      console.log(this.goodsInfo[0].sell_price)
    },
    beforeEnter(el) {
      el.style.transform = "translate(0,0)";
    },
    enter(el, done) {
      el.offsetWidth;
      //动画优化思路，导致动画不准确的原因是位移写死的
      //动态计算坐标值
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      const badgePosition = document
        .getElementById("badge")
        .getBoundingClientRect();
      const yDist = badgePosition.top - ballPosition.top;
      const xDist = badgePosition.left - ballPosition.left;

      el.style.transform = `translate(${xDist}px,${yDist}px)`;
      el.style.transition = "all 0.5s ease";
      done();
    },
    afterEnter(el) {
      this.ballflag = !this.ballflag;
    },
    getSelectedCount(count) {
      // 保存用户选中的商品数量，默认为1个
     this.selectedCount = count;
      console.log("父组件获得的值为" + selectedCount);
    }
  },
  components: {
    swipper,
    numbox
  }
};
</script>
<style lang="scss" scoped>
.detail {
  background-color: #eee;
  overflow: hidden;
}
.mint-swipe {
  height: 200px;
}
img {
  width: 100%;
  height: 100%;
}
.mui-grid-view.mui-grid-9 {
  background-color: #fff;
  border: 0;
}
.mui-grid-view.mui-grid-9 .mui-table-view-cell {
  border: 0;
}
.now {
  color: red;
  font-size: 14px;
  font-weight: bold;
}
.mui-card-footer {
  display: block;
  button {
    margin: 15px 0;
  }
}
.ball {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: red;
  position: absolute;
  z-index: 1000;
  left: 78px;
  top: 448px;
}
</style>
